package metu.tespit.session.course;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.persistence.EntityManager;

import metu.tespit.entitiy.accountManagement.User;
import metu.tespit.entity.course.Course;
import metu.tespit.session.sessionScopeManager.SessionScopeBean;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.security.Credentials;

/**

 * @created May 30, 2012
 */
@Name("peopleCourseHome")
public class PeopleCourseList {

	@In
	private EntityManager entityManager;

	@In(scope=ScopeType.SESSION)
	private SessionScopeBean sessionScopeBean;

	@In
	private Credentials credentials;

	public SessionScopeBean getSessionScopeBean() {
		return sessionScopeBean;
	}
	/**/
	@SuppressWarnings("unchecked")
	public List<Course> getInstructorResultList(){
		return (List<Course>)entityManager.createQuery("Select c from Course c where c.semester = :s and (c.instructor.username =:un or c.teachingAssistant.username =:un)")
		.setParameter("s", sessionScopeBean.getActiveSemester()).setParameter("un", credentials.getUsername()).getResultList();

	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Course> getStudentResultList(){
		User us=(User)entityManager.createQuery("Select us from User us where us.username=:un").setParameter("un", credentials.getUsername()).getSingleResult();
		List<Course> cl = us.getCoursesTaken();

		HashSet h = new HashSet( cl);
		cl.clear();
		cl.addAll(h);

		List<Course> retList=new ArrayList<Course>();
		for (Course c : cl){
			if(c.getSemester().equals(sessionScopeBean.getActiveSemester())){
				retList.add(c);
			}
		}
		return retList;

	}

}
